<?php

class Jabatan extends Controller {

	function Jabatan() {
		parent::Controller();
        $this->load->model('Jabatan_model', 'p');
	}
	
	function index() {
    	redirect("/welcome", "refresh");
	}
	
	function struktural() {
		if (!$this->session->userdata('username')) {
			$data['isLogin'] = FALSE;
        	redirect("/login", "location");
		} else {
			$data['isLogin'] = TRUE;
			$data['username'] = $this->session->userdata('username');
			$data['role'] = $this->session->userdata('role');
		}

    	$list_nip = $this->_getNIP();
    	if ($list_nip<>'') {
    		$auto_nip = '$("#nip").autocompleteArray(['.$list_nip.'], {delay:10,minChars:1,matchSubset:1,cacheLength:10,maxItemsToShow:10});'; 
    	}
		$data['additional_js'] = '
		<script type="text/javascript">
        	$(function() {
		        $("#nip").focus();'.$auto_nip.
			'});		        
			function deleteRow(id) {
				var txt = \'Anda yakin mau menghapus data jabatan ini?<input type="hidden" id="row-id" name="row-id" value="\'+ id +\'" />\';
					url = "'.base_url().'index.php/jabatan/hapus/"+id+"/struktural";
				 $.ajax({
				   type: "POST",
				   url: "'.base_url().'index.php/jabatan/jabatan_bisa_dihapus/"+id,
				   success: function(msg) {
					 if (msg==\'bisa\') {
						$.prompt(txt,{ buttons:{Hapus:true, Batal:false},
							callback: function(v){
								if(v) {
									$("tr#row-"+id).fadeOut("slow", function() {
										$("tr#row-"+id).remove();
									});
									top.location.href=url;
								}
							}
						});
					 } else {
						alert(\'Maaf, data jabatan tidak dapat dihapus dikarenakan terkait dengan data lain.\');
					 }
			       }
			    });
			}
		</script>
		';
		
		$data['judul'] = 'RIWAYAT JABATAN STRUKTURAL';
        if( $data['role'] == 0)
            $nip = $this->session->userdata('nip');
        else if ($this->uri->segment(3) == 'nip')
            $nip = $this->uri->segment(4);
        else
		    $nip = $this->input->post('nip')?$this->input->post('nip'):"";
        $this->session->set_userdata(array('nip' => $nip));    
        $data['list_data'] = $this->p->list_jabatan_struktural($nip);
        $data['panel'] = $this->_setPanelStruktural($nip);
        $data['tampilkan_menu'] = true;
        $data['jenis_jabatan'] = 'struktural';
        
        if($this->input->post('page'))echo $data['list_data'];
        else $this->load->view('view_jabatan',$data);
    }

	function fungsional() {
		if (!$this->session->userdata('username')) {
			$data['isLogin'] = FALSE;
        	redirect("/login", "location");
		} else {
			$data['isLogin'] = TRUE;
			$data['username'] = $this->session->userdata('username');
			$data['role'] = $this->session->userdata('role');
		}

    	$list_nip = $this->_getNIP();
    	if ($list_nip<>'') {
    		$auto_nip = '$("#nip").autocompleteArray(['.$list_nip.'], {delay:10,minChars:1,matchSubset:1,cacheLength:10,maxItemsToShow:10});'; 
    	}
		$data['additional_js'] = '
		<script type="text/javascript">
        	$(function() {
		        $("#nip").focus();'.$auto_nip.
			'});		        
			function deleteRow(id) {
				var txt = \'Anda yakin mau menghapus data jabatan ini?<input type="hidden" id="row-id" name="row-id" value="\'+ id +\'" />\';
					url = "'.base_url().'index.php/jabatan/hapus/"+id+"/fungsional";
				 $.ajax({
				   type: "POST",
				   url: "'.base_url().'index.php/jabatan/jabatan_bisa_dihapus/"+id,
				   success: function(msg) {
					 if (msg==\'bisa\') {
						$.prompt(txt,{ buttons:{Hapus:true, Batal:false},
							callback: function(v){
								if(v) {
									$("tr#row-"+id).fadeOut("slow", function() {
										$("tr#row-"+id).remove();
									});
									top.location.href=url;
								}
							}
						});
					 } else {
						alert(\'Maaf, data jabatan tidak dapat dihapus dikarenakan terkait dengan data lain.\');
					 }
			       }
			    });
			}
		</script>
		';
		
		$data['judul'] = 'RIWAYAT JABATAN FUNGSIONAL';
        if( $data['role'] == 0)
            $nip = $this->session->userdata('nip');
        else if ($this->uri->segment(3) == 'nip')
            $nip = $this->uri->segment(4);
        else
		    $nip = $this->input->post('nip')?$this->input->post('nip'):"";
        $data['list_data'] = $this->p->list_jabatan_fungsional($nip);
        $data['panel'] = $this->_setPanelFungsional($nip);
        $data['tampilkan_menu'] = true;
        $data['jenis_jabatan'] = 'fungsional';
        
        if($this->input->post('page'))echo $data['list_data'];
        else $this->load->view('view_jabatan',$data);
    }

    function _getPejabat() {
    	$this->db->order_by('nama', 'asc');
    	$q = $this->db->get('pejabat');
    	$list = '';
    	if ($q->num_rows() > 0) {
	    	foreach ($q->result() as $row) {
	    		$list .= '"'.$row->nama.'",';
	    	}
	    	$list = substr($list, 0, -1);
    	}
    	return $list;
    }
    
    function _getJabatan($kode) {
    	$this->db->order_by('nama', 'asc');
    	$this->db->where(array('tipe' => $kode));
    	$q = $this->db->get('jabatan');
    	$list = '';
    	if ($q->num_rows() > 0) {
	    	foreach ($q->result() as $row) {
	    		$list .= '"'.$row->nama.'",';
	    	}
	    	$list = substr($list, 0, -1);
    	}
    	return $list;
    }
    
    function _getNIP() {
    	$this->db->order_by('nip', 'asc');
    	$q = $this->db->get('pegawai');
    	$list = '';
    	if ($q->num_rows() > 0) {
	    	foreach ($q->result() as $row) {
	    		$list .= '"'.$row->nip.'",';
	    	}
	    	$list = substr($list, 0, -1);
    	}
    	return $list;
    }
    
    function _setPanelStruktural($nip) {
    	$info_pegawai = '';
    	if ($nip!="") {
            $this->db->select('gelar_depan, nama, gelar_belakang');
            $this->db->where(array('nip' => $nip));
			$this->db->or_where(array('nip_baru' => $nip));
			$query = $this->db->get('pegawai');
			if ($query->num_rows() > 0) {
				$r = $query->row();
				$info_pegawai = sprintf("%s %s %s", $r->gelar_depan, $r->nama, $r->gelar_belakang);
			} else {
				$info_pegawai = '-';
			}
		}
    	$ret =  form_open('jabatan/struktural').
				"<fieldset >
				 <table class=\"formTabel\" border=\"0\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\">
				  <tr>
				    <td class=\"kolom\" valign=\"top\">NIP : <input type=\"text\" name=\"nip\" id=\"nip\" value=\"".$nip."\" /></td>
				    <td valign=\"top\"><input class=\"tombol\" type=\"submit\" name=\"button\" id=\"button\" value=\"Tampilkan\" /></td>
				    <td valign=\"top\"><div id=\"info_pegawai\">".$info_pegawai."</div></td>
				 </tr>
				 </table>
				 </fieldset>".
				 form_close();
		return $ret;    	
    }
    
    function _setPanelFungsional($nip) {
    	$info_pegawai = '';
    	if ($nip!="") {
            $this->db->select('gelar_depan, nama, gelar_belakang');
            $this->db->where(array('nip' => $nip));
			$this->db->or_where(array('nip_baru' => $nip));
			$query = $this->db->get('pegawai');
			if ($query->num_rows() > 0) {
				$r = $query->row();
				$info_pegawai = sprintf("%s %s %s", $r->gelar_depan, $r->nama, $r->gelar_belakang);
			} else {
				$info_pegawai = '-';
			}
		}
    	$ret =  form_open('jabatan/fungsional').
				"<fieldset >
				 <table class=\"formTabel\" border=\"0\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\">
				  <tr>
				    <td class=\"kolom\" valign=\"top\">NIP : <input type=\"text\" name=\"nip\" id=\"nip\" value=\"".$nip."\" /></td>
				    <td valign=\"top\"><input class=\"tombol\" type=\"submit\" name=\"button\" id=\"button\" value=\"Tampilkan\" /></td>
				    <td valign=\"top\"><div id=\"info_pegawai\">".$info_pegawai."</div></td>
				 </tr>
				 </table>
				 </fieldset>".
				 form_close();
		return $ret;    	
    }
    
    function _setRulesStruktural() {
    	return '
                    //set the rules for the field names
                    rules: {
                        nip: { required: true },
                        jabatan: { required: true },
                        id_eselon: { required: true },
                        y_tmt: { required: true },
                        y_tgl_sk: { required: true },
                        pejabat: { required: true }
                    },
                    //set messages to appear inline
                    messages: {
                        nip: "NIP wajib diisi",
                        jabatan: "Jabatan wajib diisi",
                        id_eselon: "Eselon wajib diisi",
                        y_tmt: "Tahun TMT wajib diisi",
                        y_tgl_sk: "Tahun SK wajib diisi",
                        pejabt: "Nama Pejabat wajib diisi"
    				}
    	';
    }
    
    function _setRulesFungsional() {
    	return '
                    //set the rules for the field names
                    rules: {
                        nip: { required: true },
                        jabatan: { required: true },
                        y_tmt: { required: true },
                        y_tgl_sk: { required: true },
                        pejabat: { required: true }
                    },
                    //set messages to appear inline
                    messages: {
                        nip: "NIP wajib diisi",
                        jabatan: "Jabatan wajib diisi",
                        y_tmt: "Tahun TMT wajib diisi",
                        y_tgl_sk: "Tahun SK wajib diisi",
                        pejabt: "Nama Pejabat wajib diisi"
    				}
    	';
    }
    
    function _setJsFormStruktural($additional_string) {
    	$ret = '
		<script type="text/javascript">
        	$(function() {
		        $("#nip").focus();'.
		        $additional_string.
                '$("#btn_kembali").click( function() {
                    window.location = \''.base_url().'index.php/jabatan/struktural\';
                });
                // validate contact form on keyup and submit
                $("form").validate({
                    '.$this->_setRulesStruktural().'
                });
                $("form").submit(function() {
                	var nip = $("#nip").val();
			 		$.ajax({
					   type: "POST",
					   url: "'.base_url().'index.php/jabatan/cek_nip/"+nip,
					   success: function(msg) {
						 if (msg!=\'valid\') {
						 	$.prompt(\'Maaf, NIP "\'+nip+\'" tidak terdaftar. Silahkan periksa lagi.\'+msg);
							$("#nip").focus();
							return false;
						 }
				       }
		    		});
    			});
        	});
        </script>
    	';
    	return $ret;
    }
    
    function _setJsFormFungsional($additional_string) {
    	$ret = '
		<script type="text/javascript">
        	$(function() {
		        $("#nip").focus();'.
		        $additional_string.
                '$("#btn_kembali").click( function() {
                    window.location = \''.base_url().'index.php/jabatan/fungsional\';
                });
                // validate contact form on keyup and submit
                $("form").validate({
                    '.$this->_setRulesFungsional().'
                });
                $("input[@name=\'tipe\']").change(function() {
                	var kode_tipe = $("input[@name=\'tipe\']:checked").val();
					$.ajax({
					   type: "POST",
					   url: "'.base_url().'index.php/jabatan/getJabatan/"+kode_tipe,
					   success: function(msg) {
					   	  if (msg != \'\') {
					   	  	alert(kode_tipe);
					   	  	eval(msg);
					   	  }
				       }
				    });
                });
                $("form").submit(function() {
                	var nip = $("#nip").val();
			 		$.ajax({
					   type: "POST",
					   url: "'.base_url().'index.php/jabatan/cek_nip/"+nip,
					   success: function(msg) {
						 if (msg!=\'valid\') {
						 	$.prompt(\'Maaf, NIP "\'+nip+\'" tidak terdaftar. Silahkan periksa lagi.\'+msg);
							$("#nip").focus();
							return false;
						 }
				       }
		    		});
    			});
        	});
        </script>
    	';
    	return $ret;
    }
    
    function form_isian_struktural() {
		if (!$this->session->userdata('username')) {
			$data['isLogin'] = FALSE;
        	redirect("/login", "location");
		} else {
			$data['isLogin'] = TRUE;
			$data['username'] = $this->session->userdata('username');
			$data['role'] = $this->session->userdata('role');
		}
        
    	$list_pejabat = $this->_getPejabat();
    	if ($list_pejabat<>'') {
    		$auto_pejabat = '$("#pejabat").autocompleteArray(['.$list_pejabat.'], {delay:10,minChars:1,matchSubset:1,autoFill:true,maxItemsToShow:10});'; 
    	} else {
    		$auto_pejabat = ''; 
    	}
    	$list_nip = $this->_getNIP();
    	if ($list_nip<>'') {
    		$auto_nip = '$("#nip").autocompleteArray(['.$list_nip.'], {delay:10,minChars:1,matchSubset:1,autoFill:true,maxItemsToShow:10});'; 
    	} else {
    		$auto_nip = ''; 
    	}
    	$list_jabatan = $this->_getJabatan(TIPE_JABATAN_STRUKTURAL);
    	if ($list_jabatan<>'') {
    		$auto_jabatan = '$("#jabatan").autocompleteArray(['.$list_jabatan.'], {delay:10,minChars:1,matchSubset:1,autoFill:true,maxItemsToShow:10});';
    	} else {
    		$auto_jabatan = ''; 
    	}
    	
        $data['additional_js'] = $this->_setJsFormStruktural($auto_pejabat.$auto_nip.$auto_jabatan);
           
        $data['judul'] = 'INPUT DATA JABATAN STRUKTURAL';
        $form_action = base_url().'index.php/jabatan/simpan_struktural';
        $form_id = 'form_jabatan';
        $data['list_data'] = $this->p->entry_jabatan_struktural($form_id,$form_action);
        $this->load->view('view_jabatan', $data);
    }
    
    function form_isian_fungsional() {
		if (!$this->session->userdata('username')) {
			$data['isLogin'] = FALSE;
        	redirect("/login", "location");
		} else {
			$data['isLogin'] = TRUE;
			$data['username'] = $this->session->userdata('username');
			$data['role'] = $this->session->userdata('role');
		}
		
        $form_action = base_url().'index.php/jabatan/simpan_fungsional';
        $form_id = 'form_jabatan';
        
    	$list_pejabat = $this->_getPejabat();
    	if ($list_pejabat<>'') {
    		$auto_pejabat = '$("#pejabat").autocompleteArray(['.$list_pejabat.'], {delay:10,minChars:1,matchSubset:1,autoFill:true,maxItemsToShow:10});'; 
    	} else {
    		$auto_pejabat = ''; 
    	}
    	$list_nip = $this->_getNIP();
    	if ($list_nip<>'') {
    		$auto_nip = '$("#nip").autocompleteArray(['.$list_nip.'], {delay:10,minChars:1,matchSubset:1,autoFill:true,maxItemsToShow:10});'; 
    	} else {
    		$auto_nip = ''; 
    	}
    	$list_jabatan = $this->_getJabatan(TIPE_JABATAN_FUNGSIONAL_AK);
    	if ($list_jabatan<>'') {
    		$auto_jabatan = '$("#jabatan").autocompleteArray(['.$list_jabatan.'], {delay:10,minChars:1,matchSubset:1,autoFill:true,maxItemsToShow:10});';
    	} else {
    		$auto_jabatan = ''; 
    	}
    	
        $data['additional_js'] = $this->_setJsFormFungsional($auto_pejabat.$auto_nip.$auto_jabatan);
           
        $data['judul'] = 'INPUT DATA JABATAN FUNGSIONAL';
        $data['list_data'] = $this->p->entry_jabatan_fungsional($form_id,$form_action);
        $this->load->view('view_jabatan', $data);
    }
    
    function simpan_struktural($is_update=FALSE, $id='') {
        $this->load->model('Datum_model', 'd');
    	if (!$this->input->post('nip')) {
            redirect('jabatan', 'refresh');
        } else {
        	
            $id_pegawai = $this->d->getData('idspg_pegawai', 'pegawai', array('nip_baru' => $this->input->post('nip')));
            $arr['id_pegawai'] = $id_pegawai!=false?$id_pegawai:$this->d->getData('idspg_pegawai', 'pegawai', array('nip' => $this->input->post('nip')));
        }   

        if ($this->input->post('jabatan')) {
	        $jabatan = $this->input->post('jabatan')?$this->input->post('jabatan'):"";
	        $id_jabatan = $this->d->getData('idspg_jabatan', 'jabatan', array('nama' => $jabatan, 'tipe' => TIPE_JABATAN_STRUKTURAL));
	        if ($id_jabatan) {
	        	$arr['id_jabatan'] = $id_jabatan;
	        } else {
	        	$this->db->insert('jabatan', array('nama' => $jabatan, 'tipe' => TIPE_JABATAN_STRUKTURAL));
	        	$arr['id_jabatan'] = $this->db->insert_id();
	        }
        } else {
        	$data['judul'] = 'ERROR';
        	$data['isi']   = 'Field JABATAN tidak valid.';
        	$data['link_kembali'] = base_url().'index.php/jabatan/struktural';
        	$this->load->view('view_blank', $data);
        }
        
        $arr['id_eselon'] = $this->input->post('id_eselon')?$this->input->post('id_eselon'):1;
        if ($this->input->post('d_tmt')) {
        	$a = array($this->input->post('y_tmt'), $this->input->post('m_tmt'), $this->input->post('d_tmt'));
            $arr['tmt'] = implode(TGL_SEPARATOR, $a);
        } else {
            $arr['tmt'] = date("Y-m-d");
        }
        if ($this->input->post('d_tgl_sk')) {
        	$a = array($this->input->post('y_tgl_sk'), $this->input->post('m_tgl_sk'), $this->input->post('d_tgl_sk'));
            $arr['tgl_sk'] = implode(TGL_SEPARATOR, $a);
        } else {
            $arr['tgl_sk'] = date("Y-m-d");
        }
        $arr['no_sk'] = $this->input->post('no_sk')?$this->input->post('no_sk'):"";
        $arr['gaji_pokok'] = $this->input->post('gaji_pokok')?$this->input->post('gaji_pokok'):"0";
        $arr['namauser'] = $this->session->userdata('username');
        
        if ($this->input->post('pejabat')) {
        	$id_pejabat = $this->d->getData('idspg_pejabat', 'pejabat', array('nama' => $this->input->post('pejabat')));
        	if ($id_pejabat) {
            	$arr['id_pejabat'] = $id_pejabat;
        	} else {
        		$this->db->insert('pejabat', array('nama' => $this->input->post('pejabat')));
            	$arr['pejabat'] = $this->db->insert_id();
        	}
        } else {
        	$data['judul'] = 'ERROR';
        	$data['isi']   = 'Field PEJABAT tidak valid.';
        	$data['link_kembali'] = base_url().'index.php/jabatan/struktural';
        	$this->load->view('view_blank', $data);
        }
        
        #if ($is_update) {
        	$this->p->simpan($arr, $is_update, $id);
        	redirect('jabatan/struktural/nip/'.$this->input->post('nip'), 'refresh');
        /*} else {
        	$this->p->simpan($arr);
        	redirect('jabatan/form_isian_struktural', 'refresh');
        }*/
    }
    
    function simpan_fungsional($is_update=FALSE, $id='') {
        $this->load->model('Datum_model', 'd');
    	if (!$this->input->post('nip')) {
            redirect('jabatan/fungsional', 'refresh');
        } else {
        	$id_pegawai = $this->d->getData('idspg_pegawai', 'pegawai', array('nip_baru' => $this->input->post('nip')));
            $arr['id_pegawai'] = $id_pegawai!=false?$id_pegawai:$this->d->getData('idspg_pegawai', 'pegawai', array('nip' => $this->input->post('nip')));
        }

        if ($this->input->post('jabatan')) {
	        $jabatan = $this->input->post('jabatan')?$this->input->post('jabatan'):"";
	        $tipe    = $this->input->post('tipe')?$this->input->post('tipe'):TIPE_JABATAN_FUNGSIONAL_AK; 
	        $id_jabatan = $this->d->getData('idspg_jabatan', 'jabatan', array('nama' => $jabatan, 'tipe' => $tipe));
	        if ($id_jabatan) {
	        	$arr['id_jabatan'] = $id_jabatan;
	        } else {
	        	$this->db->insert('jabatan', array('nama' => $jabatan, 'tipe' => $tipe));
	        	$arr['id_jabatan'] = $this->db->insert_id();
	        }
        } else {
        	$data['judul'] = 'ERROR';
        	$data['isi']   = 'Field JABATAN tidak valid.';
        	$data['link_kembali'] = base_url().'index.php/jabatan/fungsional';
        	$this->load->view('view_blank', $data);
        }
        
        $arr['id_golongan'] = $this->input->post('id_golongan')?$this->input->post('id_golongan'):1;
        if ($this->input->post('d_tmt')) {
        	$a = array($this->input->post('y_tmt'), $this->input->post('m_tmt'), $this->input->post('d_tmt'));
            $arr['tmt'] = implode(TGL_SEPARATOR, $a);
        } else {
            $arr['tmt'] = date("Y-m-d");
        }
        if ($this->input->post('d_tgl_sk')) {
        	$a = array($this->input->post('y_tgl_sk'), $this->input->post('m_tgl_sk'), $this->input->post('d_tgl_sk'));
            $arr['tgl_sk'] = implode(TGL_SEPARATOR, $a);
        } else {
            $arr['tgl_sk'] = date("Y-m-d");
        }
        $arr['no_sk'] = $this->input->post('no_sk')?$this->input->post('no_sk'):"";
        $arr['gaji_pokok'] = $this->input->post('gaji_pokok')?$this->input->post('gaji_pokok'):"0";
        $arr['namauser'] = $this->session->userdata('username');
        
        if ($this->input->post('pejabat')) {
        	$id_pejabat = $this->d->getData('idspg_pejabat', 'pejabat', array('nama' => $this->input->post('pejabat')));
        	if ($id_pejabat) {
            	$arr['id_pejabat'] = $id_pejabat;
        	} else {
        		$this->db->insert('pejabat', array('nama' => $this->input->post('pejabat')));
            	$arr['pejabat'] = $this->db->insert_id();
        	}
        } else {
        	$data['judul'] = 'ERROR';
        	$data['isi']   = 'Field PEJABAT tidak valid.';
        	$data['link_kembali'] = base_url().'index.php/jabatan/fungsional';
        	$this->load->view('view_blank', $data);
        }
        
        #if ($is_update) {
        	$this->p->simpan($arr, $is_update, $id);
        	redirect('jabatan/fungsional/nip/'.$this->input->post('nip'), 'refresh');
        /*} else {
        	$this->p->simpan($arr);
        	redirect('jabatan/form_isian_fungsional', 'refresh');
        }*/
    }
    
    function form_edit_struktural($id) {
		if (!$this->session->userdata('username')) {
			$data['isLogin'] = FALSE;
        	redirect("/login", "location");
		} else {
			$data['isLogin'] = TRUE;
			$data['username'] = $this->session->userdata('username');
			$data['role'] = $this->session->userdata('role');
		}
		
        $this->load->model('Datum_model', 'd');
    	$list_pejabat = $this->_getPejabat();
    	if ($list_pejabat<>'') {
    		$auto_pejabat = '$("#pejabat").autocompleteArray(['.$list_pejabat.'], {delay:10,minChars:1,matchSubset:1,autoFill:true,maxItemsToShow:10});'; 
    	} else {
    		$auto_pejabat = ''; 
    	}
    	$list_nip = $this->_getNIP();
    	if ($list_nip<>'') {
    		$auto_nip = '$("#nip").autocompleteArray(['.$list_nip.'], {delay:10,minChars:1,matchSubset:1,autoFill:true,maxItemsToShow:10});'; 
    	} else {
    		$auto_nip = ''; 
    	}
    	$list_jabatan = $this->_getJabatan(TIPE_JABATAN_STRUKTURAL);
    	if ($list_jabatan<>'') {
    		$auto_jabatan = '$("#jabatan").autocompleteArray(['.$list_jabatan.'], {delay:10,minChars:1,matchSubset:1,autoFill:true,maxItemsToShow:10});';
    	} else {
    		$auto_jabatan = ''; 
    	}
    	    		    	
        $data['additional_js'] = $this->_setJsFormStruktural($auto_pejabat.$auto_nip.$auto_jabatan);

        $this->db->where(array('idspg_pengangkatan_jabatan' => $id));
        $q = $this->db->get('pengangkatan_jabatan');
        if ($q->num_rows() > 0) {
            $row = $q->row();
            $nip2 = $this->d->getData('nip_baru', 'pegawai', array('idspg_pegawai' => $row->id_pegawai));
            $arr['nip']                  = $nip2!=''?$nip2:$this->d->getData('nip', 'pegawai', array('idspg_pegawai' => $row->id_pegawai));
            $arr['jabatan']              = $this->d->getData('nama', 'jabatan', array('idspg_jabatan' => $row->id_jabatan));
            $arr['id_eselon']            = $row->id_eselon;
            $arr['no_sk']                = $row->no_sk;
            $arr['gaji_pokok']           = $row->gaji_pokok;
            $arr['pejabat']              = $this->d->getData('nama', 'pejabat', array('idspg_pejabat' => $row->id_pejabat));
            $arr['tgl_sk']               = $row->tgl_sk;
            $arr['tmt']                  = $row->tmt;
            
            $form_action = base_url().'index.php/jabatan/simpan_struktural/true/'.$id;
            $form_id = 'form_jabatan';
            
            $data['judul'] = 'UBAH DATA JABATAN STRUKTURAL PEGAWAI';
            $data['list_data'] = $this->p->entry_jabatan_struktural($form_id,$form_action,$arr);
            $this->load->view('view_jabatan', $data);
        } else {
        	$data['judul'] = 'ERROR';
        	$data['isi']   = 'Internal ID error.';
        	$data['link_kembali'] = base_url().'index.php/jabatan/struktural';
        	$this->load->view('view_blank', $data);
        }
    }

    function form_edit_fungsional($id) {
		if (!$this->session->userdata('username')) {
			$data['isLogin'] = FALSE;
        	redirect("/login", "location");
		} else {
			$data['isLogin'] = TRUE;
			$data['username'] = $this->session->userdata('username');
			$data['role'] = $this->session->userdata('role');
		}
		
        $this->load->model('Datum_model', 'd');
    	$list_pejabat = $this->_getPejabat();
    	if ($list_pejabat<>'') {
    		$auto_pejabat = '$("#pejabat").autocompleteArray(['.$list_pejabat.'], {delay:10,minChars:1,matchSubset:1,autoFill:true,maxItemsToShow:10});'; 
    	} else {
    		$auto_pejabat = ''; 
    	}
    	$list_nip = $this->_getNIP();
    	if ($list_nip<>'') {
    		$auto_nip = '$("#nip").autocompleteArray(['.$list_nip.'], {delay:10,minChars:1,matchSubset:1,autoFill:true,maxItemsToShow:10});'; 
    	} else {
    		$auto_nip = ''; 
    	}
    	$list_jabatan = $this->_getJabatan(TIPE_JABATAN_STRUKTURAL);
    	if ($list_jabatan<>'') {
    		$auto_jabatan = '$("#jabatan").autocompleteArray(['.$list_jabatan.'], {delay:10,minChars:1,matchSubset:1,autoFill:true,maxItemsToShow:10});';
    	} else {
    		$auto_jabatan = ''; 
    	}
    	    		    	
        $data['additional_js'] = $this->_setJsFormFungsional($auto_pejabat.$auto_nip.$auto_jabatan);
                
        $this->db->where(array('idspg_pengangkatan_jabatan' => $id));
        $q = $this->db->get('pengangkatan_jabatan');
        if ($q->num_rows() > 0) {
            $row = $q->row();
            $nip2 = $this->d->getData('nip_baru', 'pegawai', array('idspg_pegawai' => $row->id_pegawai));
            $arr['nip']                  = $nip2!=''?$nip2:$this->d->getData('nip', 'pegawai', array('idspg_pegawai' => $row->id_pegawai));
            $arr['jabatan']              = $this->d->getData('nama', 'jabatan', array('idspg_jabatan' => $row->id_jabatan));
            $arr['tipe']                 = $this->d->getData('tipe', 'jabatan', array('idspg_jabatan' => $row->id_jabatan));
            $arr['id_golongan']          = $row->id_golongan;
            $arr['no_sk']                = $row->no_sk;
            $arr['gaji_pokok']           = $row->gaji_pokok;
            $arr['pejabat']              = $this->d->getData('nama', 'pejabat', array('idspg_pejabat' => $row->id_pejabat));
            $arr['tgl_sk']               = $row->tgl_sk;
            $arr['tmt']                  = $row->tmt;
            
            $form_action = base_url().'index.php/jabatan/simpan_fungsional/true/'.$id;
            $form_id = 'form_jabatan';
            
            $data['judul'] = 'UBAH DATA JABATAN FUNGSIONAL';
            $data['list_data'] = $this->p->entry_jabatan_fungsional($form_id,$form_action,$arr);
            $this->load->view('view_jabatan', $data);
        } else {
        	$data['judul'] = 'ERROR';
        	$data['isi']   = 'Internal ID error.';
        	$data['link_kembali'] = base_url().'index.php/jabatan/fungsional';
        	$this->load->view('view_blank', $data);
        }
    }

    function hapus($id,$jenis) {
        $this->db->where('idspg_pengangkatan_jabatan', $id);
        $this->db->delete('pengangkatan_jabatan');
        redirect('jabatan/'.$jenis, 'refresh');
    }
    
    
    function cek_nip($nip='') {
    	if ($nip != '') {
	        $this->load->model('Datum_model', 'd');
            $ret = $this->d->getData('nip', 'pegawai', array('nip' => $nip));
	    	$ret2 = $this->d->getData('nip_baru', 'pegawai', array('nip_baru' => $nip));
	    	if ($ret!=false || $ret2!=false) {
	    		echo 'valid';
	    	} else {
	    		echo 'ellol';
	    	}
    	} else {
    		echo 'ellol';
    	}
    }
    
    
    function jabatan_bisa_dihapus($id) {
    	echo 'bisa';
    }
    
    
    function getJabatan($kode) {
    	$this->db->order_by('nama', 'asc');
    	$this->db->where(array('tipe' => $kode));
    	$q = $this->db->get('jabatan');
    	$list = '';
    	if ($q->num_rows() > 0) {
	    	foreach ($q->result() as $row) {
	    		$list .= '"'.$row->nama.'",';
	    	}
	    	$list = substr($list, 0, -1);
    	}
    	
    	if ($list != '') {
	    	echo '$("#jabatan").autocompleteArray([ '.$list.' ], {delay:10,minChars:1,matchSubset:1,autoFill:true,maxItemsToShow:10});';
    	} else {
    		echo '';
    	} 
    }
    
    
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */